package hr_project.hr.web.Emp;

import java.util.List;

import javax.servlet.http.HttpSession;

import org.apache.ibatis.annotations.Param;
import org.junit.runners.Parameterized.Parameters;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import hr_project.hr.biz.Dept.IDeptBiz;
import hr_project.hr.entity.Department;
import hr_project.hr.entity.Emp;
import hr_project.hr.util.pagebean;

@Controller
@RequestMapping("/dept")
public class DeptAction {

	@Autowired
	private  IDeptBiz biz;

	public IDeptBiz getBiz() {
		return biz;
	}

	public void setBiz(IDeptBiz biz) {
		this.biz = biz;
	}
	
	
	
	//列表
	@ResponseBody
	@RequestMapping("/list")
	public List<Department> getAll()
	{
		return biz.getAll();
	}
	
	 //分页列表
		@ResponseBody
		@RequestMapping("/page-list")
		public pagebean getAllBypage(@RequestParam(defaultValue="1")int page)
		{
			pagebean pb=biz.getAllBypage(page);
			return pb;
		}
	
	//增加
	@RequestMapping("/add")
	public String addDept(Department dept,HttpSession sess)
	{
		Emp e=(Emp)sess.getAttribute("loger");
		dept.setDe_changer(e);
		biz.addDept(dept);
		return "dept-list";
	}
	
	//修改前获取一个
	@RequestMapping("/getbyid")
	public ModelAndView getByid(long id,int n)
	{
		ModelAndView mv=new ModelAndView();
		Department dept=biz.getByid(id);
		if(n==1)
		{
			mv.setViewName("apply-ModDept");
		}else if(n==2)
		{
			mv.setViewName("dept-mod");
		}else
		{
			mv.setViewName("apply-dept");
		}
		mv.addObject("dept", dept);
		return mv;
		
	}
	
	//申请修改
	@RequestMapping("/apply-mod")
	public String modDept(Department dept,HttpSession sess)
	{
		Emp e=(Emp)sess.getAttribute("loger");
		if(e!=null) System.out.println(e.getUs_id());
		dept.setDe_changer(e);
		biz.modDept(dept);
		return "dept-list";
	}
	
	//申请未通过
	@RequestMapping("/del")
	public String delDept(long id,String status,String de_comm,HttpSession sess)
	{
		
		if(status.equals("待增加"))
		{
		    biz.delDept(id);
		}else
		{
			Department dept=biz.getByid(id);
			Emp e=(Emp)sess.getAttribute("loger");
			dept.setDe_changer(e);
			dept.setStatus("正常");
			dept.setDe_comm(de_comm);
			biz.modDept(dept);
		}
		return "dept-list";
	}
	
	//所有非正常状态下的部门
	@ResponseBody
	@RequestMapping("/apply-list")
	public List<Department> getAllNotnormal()
	{
		List<Department> list=biz.getAllNotnormal();
		return list;
	}
	
	//审核
	@RequestMapping("/apply-dept")
	public String apply(long id,String status)
	{
		Department dept=biz.getByid(id);
		dept.getDe_changer().getUs_id();
		System.out.println("更改人"+dept.getDe_changer().getUs_name());
		if(status.equals("待增加"))
		{
			dept.setStatus("正常");	
		}else if(status.equals("申请删除"))
		{
			dept.setStatus("已删除");	
		}else if(status.equals("申请修改"))
		{
			dept.setStatus("待修改");
		}
		biz.modDept(dept);
		return "dept-apply-list";
	}
	
	//恢复
	@RequestMapping("/start")
	public String start(long id)
	{
		Department dept=biz.getByid(id);
		dept.setStatus("正常");
		biz.modDept(dept);
		return "dept-apply-list";
	}
}
